/**
 * 订单投标记录详情
 */
var table = null;
$(document).ready(function () {
        table = $('#bid_record_table').dataTableExtend({
            "ajax": {"url": sysInfo.basePath+"/sc/general/bid/queryBidList.do", "type": "post"},
            "fnServerParams": function (aoData) {
                aoData = $.extend(aoData, {scProductOrderId: $('#mainId').val()});
                return aoData;
            },
            "scrollX": "true",//是否开启横项滚动
            "fixedColumns": {//锁定列，只有开启scrollX时才生效
                "leftColumns": 0,
                "rightColumns": 0
            },
            "aoColumns": [
            	{
                    "orderable": false,
                    "data": "id",
                    "render": function (data, type, full, meta) {
                        return '<input type="checkbox"  name="ids" class="checkchild"  value="' + data + '" />';
                    }
                }
               , {
                   "orderable": false,
                   "render": function (data, type, full, meta) {
                       return meta.row + 1;
                   }
               }, {
                    "orderable": false,
                    "data": "bidUserName"
                }, {
                    "orderable": false,
                    "data": "workTime"
                },
                {
                    "orderable": false,
                    "data": "workTimeCost"
                },
                {
                    "orderable": false,
                    "data": "commission"

                },
                {
                    "orderable": false,
                    "data": "allCost"
                },
                {
                    "orderable": false,
                    "data": "contractPercentage",
                    "render": function (data, type, full, meta) {
                    	var money = parseInt($("#money").val());
                    	var cost = parseInt(full.allCost);
                    	if(isNaN(money)){
                    		return "";
                    	}else{
                    		return ((cost/money)*100).toFixed(2)+"%";
                    	}
                    }
                }, 
                {
                    "orderable": false,
                    "data": "joiners",
                    "render": function (data, type, full, meta) {
                    	if(data!=null){
                    		return data.replace(/,/g, '<br/>');
                    	}else{
                    		return data;
                    	}
                    }
                },
                {
                    "orderable": false,
                    "data": "id",
                    "render": function (data, type, full, meta) {
                    	return data;
                    }
                },
                {
                    "orderable": false,
                    "data": "remark",
                    "render": function (data, type, full, meta) {
                    	if(data.length>10){
                    		return '<p title="'+data+'">'+data.substring(0,9)+"......"+'</p>';
                    	}
                    	else{
                    		return data?data:"";
                    	}
                    }
                },
                {
                    "orderable": false,
                    "data": "status",
                    "render": function (data, type, full, meta) {
                    	if(data==1){
                    		return "已中标";
                    	}else{
                    		return "未中标";
                    	}
                    }
                },
                {
                    "orderable": false,
                    "data": "createTime"
                }],
            "columnDefs": [{
                targets: [4], //第1，2，3列
                createdCell: function (td, cellData, rowData, row, col) {
                    var nodeSt = rowData.nodeultratime;
                    var orderSt = rowData.productultratime;
                    if (nodeSt == "OVERDUE_STATE2") {
                        $(td).parent().addClass(" bgOver")
                    }
                    if (orderSt == "OVERDUE_STATE2") {
                        $(td).parent().removeClass("bgOver").addClass(" bgRed")
                    }
                }
            },{
            	targets: [9], //第1，2，3列
                createdCell: function (td, cellData, rowData, row, col) {
                	var $td = $(td), id = $td.text();
                	$td.empty().append(getUploadLab(id));
                }
            }]
        });
        /**
         * 附件
         * @returns
         */
        var dggUploader = $(".upload").dggUploader({
            fileId: $("#bidFieldId").val()//文件ID
            , uploadBaseUrl: uploadBaseUrl
           , isRemove:false
            , showIcon: true
        });
        dggUploader.findFiles($("#bidFieldId").val(), "bidFilePreview", 0, 0);//获取文件列表
});
/**
 * 获取表格数据
 * @returns
 */
function getSelectData() {
    return $('#bid_record_table').dataTableExtend('getCheck');
}
/*
 * 选择中标
 */
function select(){
	var data = getSelectData();
	if(data.length==0){
		DGG.Layer.message("请选择1条记录",{icon: '2'});
		return false;
	}
	if(data.length>1){
		DGG.Layer.message("只能选1条记录",{icon: '2'});
		return false;
	}
	var id = data[0].id;
	DGG.Layer.loading.start();
	var data = $("#data-form").serialize();
	$.ajax({
        url: sysInfo.basePath + '/sc/general/bid/doSelectBid.do',
        data: {
        	"id":id,
        	"scProductOrderId":$('#mainId').val()
        },
        type: "POST",
        dataType: "json",
        success: function (obj) {
        	DGG.Layer.loading.done();
            if (obj.code != 0) {
                DGG.Layer.message(obj.msg, {icon: '2'}, function () {
                    window.parent.dataTableReload();
                });
            } else {
                DGG.Layer.message('操作成功！', {icon: '1'}, function () {
                    window.parent.dataTableReload();
                    parent.layer.closeAll();
                });
            }
        }
    });
}
/*
 * 分配
 */
function allot(){
	DGG.Layer.open(sysInfo.basePath + "/sc/general/bid/toAllot.do?id=" + $('#mainId').val(), {
        title: '分配订单',
        area: ['80%', '80%'],
        btn:''
    });
}
/*
 * 退出弹出层
 */
function closeLayer(){
	window.parent.dataTableReload();
    parent.layer.closeAll();
}
function getUploadLab(id){
	var str ="";
	str='<div class="upload-bz" style="display:inline">'
    +'<input type="hidden" name="fileId" value="sccenter_general_'+id+'_scbidrecord_bid_bidFileId" id="bidFieldId'+id+'">'
    +'<ul class="data-warp" id="bidFilePreview'+id+'" style="display:inline">'
       +'<li class="upload-re">'
            +'<a href="javascript:void(0)" class="upload-button upload"></a>'
            +'<p>上传附件</p>'
        +'</li>'
    +'</ul>'
    +'</div>';
	var $upload = $(str);
	$upload.find('.upload').dggUploader({
        fileId: $upload.find('input[name="fileId"]').val(),//文件ID
        uploadBaseUrl: uploadBaseUrl,
        isRemove:false,
        showIcon: true
    }).findFiles($upload.find('input[name="fileId"]').val(), 'bidFilePreview' + id, 0, 0);
	$upload.find("li[class='upload-re']").remove();
    return $upload[0];
}
